
$(function () {
    //页面加载时初始化
    alarmInit();
    //查询
    $("#query").click(function() {
        selectPage(1);
    });
})

function alarmInit() {
    if (insIpStr != null && insIpStr != '') {
        var insIps = insIpStr.split(",");
        // var managerIp = $("#managerIp").val();
        for (var i = 0; i < insIps.length; i++) {
            $("#insIp").append('<option value="'+insIps[i]+'">'+insIps[i]+'</option>');
            // if(managerIp==insIps[i]){
            //     $("#insIp").append('<option value="'+insIps[i]+'" selected="selected">'+insIps[i]+'</option>');
            // }else{
            //     $("#insIp").append('<option value="'+insIps[i]+'">'+insIps[i]+'</option>');
            // }
        }
    }
    //加载已经选择的告警状态
    if(alarmState!=null&&alarmState!=''){
        $("#alarm_state").find("option").each(function(){
            if($(this).val()==alarmState){
                $(this).attr("selected",true);
            }
        });
    }
    //加载已经选择的告警级别
    $("input[name='level']").each(function() {
        if($(this).val() == level){
            $(this).attr("checked", true);
        }
    });
    //加载分页（加载分页第一步是获取查询条件，因此这一步必须放在初始化最后）
    selectPage(1);
}

function selectPage(pageNo){
    var insIp = $("#insIp").val();
    var startTime = $("#startTime").val().trim();
    var endTime = $("#endTime").val().trim();
    var alarmState = $("#alarm_state").val();
    var alarmType = $("#alarm_type").val();
    var level = "";
    $("input[name='level']:checked").each(function () {
        level += $(this).val() + ",";
    });
    if (level.length > 0) {
        level = level.substring(0, level.length - 1);
    };
    var pageSize = $("#pageSize  option:selected").text()==""? 15:$("#pageSize  option:selected").text();

    var columns =[{code:'alarmCollectId',name:'checkBox',width:10},
        {code:'insIp',name:'告警来源'},
        {code:'insName',name:'资源名称'},
        {code:'alarmLevelStr',name:'告警级别'},
        {code:'alarmTypeStr',name:'告警类型'},
        {code:'alarmStateStr',name:'告警状态'},
        {code:'indicatorName',name:'指标名称'},
        {code:'alarmNumber',name:'告警次数'},
        {code:'ignoreNumber',name:'忽略次数'},
        {code:'firstTimeStr',name:'第一次告警'},
        {code:'lastTimeStr',name:'最近一次告警'},
        {code:'alarmMessage',name:'消息'},
        {code : 'S_G',name : '操作'}];
    var  parm={url:basePath+'/radar/alarmControl/pagingAlarmCollect.do',divID:'alarmControl', afterQuery:"updateBtn"};// 常规参数
    var  data={pageNo:pageNo, pageSize:pageSize, insIp:insIp, startTime:startTime, endTime:endTime, alarmState:alarmState, alarmType:alarmType, level:level}; // 筛选条件
    easyQuery(columns,parm,data);
}

//页面加载完之后隐藏类型为“已解决”告警信息，将类型为“已忽略”告警信息的按钮改为“取消忽略”
function updateBtn() {
    $("td[title = '已解决']").parent().find("i:eq(1)").each(function () {
        $(this).hide(0);
    });
    $("td[title = '已忽略']").parent().find("i:eq(1)").each(function () {
        $(this).css("opacity", "0.4").attr("title", "取消忽略");
    })
}

function see_alarmControl(btn) {
    var alarmId = $(btn).parent().parent().attr("nodeId");
    layer.open({
        extend:['skin/layer_iframe.css'],
        skin:'layer-ext-skin-layer_iframe.css',
        type:2,
        title:'查看详细数据',
        area:['800px','500px'],
        content:'showDetails.do?alarmId='+alarmId,
        maxmin: true,
        shadeClose: true
    });

}

//列表中的忽略按钮
function ign_alarmControl(btn){
    var alarmId = $(btn).parent().parent().attr("nodeId");
    ignore(btn, alarmId);
}

//忽略或取消忽略按钮
function allIgnore(btn){
    var alarmCollectIds="";

    $("input:[type='checkbox']:checked").each(function() {//获取选中的告警信息
        //如果选中按钮中包含“已解决”，则返回
        var state = $(this).parent().parent().find("td:eq(5)").attr("title");
        if (state == "已解决"){
            return;
        };
        var alarmId = $(this).attr("nodeId");
        if (alarmId != null && alarmId != "") {
            alarmCollectIds = alarmCollectIds + $(this).attr("nodeId") + ",";
        }
    });
    if(alarmCollectIds.length>0){
        ignore(btn,alarmCollectIds.substring(0,alarmCollectIds.length-1));
    }else{
        layer.msg("没有需要忽略的告警！");
    }
}

function ignore(btn, alarmCollectIds) {
    var ignoreState;
    if ($(btn).text() == "忽略" || $(btn).attr("title") == "忽略") { //页面右上角的“忽略”按钮
        ignoreState = 2;
    }else { //页面右上角的“取消忽略”按钮
        ignoreState = 0;
    }
    $.ajax({
        url:basePath+"/radar/alarmControl/alarmIgnore.do",
        type:"POST",
        dataType:"json",
        data:{
            "alarmIds":alarmCollectIds,
            "state":ignoreState
        },
        success:function(result){
            if(result.code == "200"){
                if (ignoreState == 2) {
                    layer.msg("忽略成功!",{time:1000});
                }else{
                    layer.msg("取消忽略成功!",{time:1000});
                }
                $("input:[type='checkbox']:checked").each(function () {
                    $(this).removeAttr("checked");
                })
            }
            selectPage(1);
        }
    });
}



